<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use Think\Config;
use think\File;
/*
后台通用提交
*/
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://115cms.com.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 作者@梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Api extends Common
{
    //批量删除
    public function delss()
    {
        hook('phpadmindelss');
        //权限
        if (socha(user('tid'), 1) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = Db::name(input('model'))->delete($ids);
        if ($result) {
            return json(array('code' => 1, 'msg' => '批量删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '批量删除失败'));
        }
    }
    //单个删除
    public function catedelete()
    {
        hook('phpadmincatedelete');
        //权限
        if (socha(user('tid'), 2) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        if (Db::name(ps(input('model')))->delete(array(get('i', 'id') => $id))) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //通用添加内容
    public function add()
    {
        hook('phpadminadd');
        //权限
        if (socha(user('tid'), 3) != 1) {
            $this->error('权限不足');
        }
        $_data = $this->request->post();
        $_data['pics'] = json_encode($_data['pics']);
        $_data['key'] = generate_password(16);
        $_data['uid'] = user('userid');
        $_data['time'] = time();
        $_data['times'] = time();
        if (!Db::name(ps(input('model')))->strict(false)->data($_data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //通用更新内容
    public function edit()
    {
        hook('phpadminedit');
        //权限
        if (socha(user('tid'), 4) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $data = $this->request->post();
        $data['pics'] = json_encode($data['pics']);
        $data['times'] = time();
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //文章内容更新
    public function edits()
    {
        hook('phpadminedits');
        //权限
        if (socha(user('tid'), 5) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $c = Db::name(input('model'))->where(array('id'=>$id))->find();
        $data = $this->request->post();
        $data['pics'] = json_encode($data['pics']);
        $data['times'] = time();
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //添加member用户
    public function add_member()
    {
        hook('phpadminaddmember');
        //权限
        if (socha(user('tid'), 6) != 1) {
            $this->error('权限不足');
        }
        $data = $this->request->post();
        $data['reg_time'] = time();
        $data['userip'] = $_SERVER['REMOTE_ADDR'];
        $key = 'www.115cms.com';
        if (empty(input('password'))) {
            $data['password'] = jiami('123456', 'ENCODE', $key, 0);
        } else {
            $data['password'] = jiami(input('password'), 'ENCODE', $key, 0);
        }
        if (empty($_POST['tid'])) {
            $data['tid'] = 0;
        } else {
            $tid = $_POST['tid'];
            $data['tid'] = implode(',', $tid);
        }
        if (Db::name(ps(input('model')))->strict(false)->data($data)->insert()) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }
    //更新内容member用户
    public function edit_member()
    {
        hook('phpadmineditmember');
        //权限
        if (socha(user('tid'), 7) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $data = $this->request->post();
        $data['times'] = time();
        $key = 'www.115cms.com';
        if (!empty(input('password'))) {
            $data['password'] = jiami(input('password'), 'ENCODE', $key, 0);
        } else {
            $data['password'] = fun_sc_p(ps(input('model')), array(get('i', 'id') => $id), 'password');
        }
        if (empty($_POST['tid'])) {
            $data['tid'] = 0;
        } else {
            $tid = $_POST['tid'];
            $data['tid'] = implode(',', $tid);
        }
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //一键更新-首页-公用
    public function uindex()
    {
        hook('phpadminuindex');
        //权限
        if (socha(user('tid'), 8) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $or = input('or');
        $data = $this->request->post();
        $s = Db::name(ps(input('model')))->where(get('i', 'id'), $id)->find();
        if ($s[$or] == 1) {
            Db::name(ps(input('model')))->where(get('i', 'id'), $id)->update([$or => 0]);
            return json(array('code' => 1, 'msg' => '更新成功', 'or' => 1));
        } else {
            Db::name(ps(input('model')))->where(get('i', 'id'), $id)->update([$or => 1]);
            return json(array('code' => 1, 'msg' => '更新成功', 'or' => 2));
        }
    }
    // 删除附件
    public function filedelete()
    {
        hook('phpadminfiledelete');
        //权限
        if (socha(user('tid'), 9) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $m = input('m');
        $time =fun_sc_p('file', array('id'=>$id), 'create_time');
        $savename =fun_sc_p('file', array('id'=>$id), 'savename');
        $thumb=fun_sc_p('file', array('id'=>$id), 'sha1');
        $ext=fun_sc_p('file', array('id'=>$id), 'ext');
        $data = $this->request->get();
        if (Db::name('file')->where('id', $id)->data($data)->delete($data)) {
            unlink(ROOT_PATH . '/uploads/'.date('Ymd',$time).'/'.$savename);
            unlink(ROOT_PATH . '/uploads/thumb/'.$thumb.'thumb.'.$ext);
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //更新缓存
    function update()
    {
        hook('phpadminupdete');
        //权限
        if (socha(user('tid'), 10) != 1) {
            $this->error('权限不足');
        }
        $res = deleteun(ROOT_PATH . 'runtime/cache/');
        if ($res) {
            array_map('unlink', glob(TEMP_PATH . '/*.php'));
            rmdir(TEMP_PATH);
            return json(array('code' => 1, 'msg' => '更新缓存成功'));
        } else {
            return json(array('code' => 0, 'msg' => '更新缓存失败'));
        }
    }
    //独立输出文章内容
    public function at()
    {
        hook('phpadminat');
        if (input('id')) {
            $c = Db::name(get('m'))->find(get('id',0));
            echo $c['content'];
        }
    }
    //重置内容获取
    public function cz()
    {
        hook('phpadmincz');
        if (input('id')) {
            $c = Db::name(get('m'))->find(get('id',0));
            return json(array('code' => 1, 'msg' => '获取成功' ,'com'=>$c['content']));
        }
    }
    // 设置模板
    public function muban()
    {
        hook('phpadminmuban');
        //权限
        if (socha(user('tid'), 11) != 1) {
            $this->error('权限不足');
        }
        if (request()->isPost()) {
            $config = $this->request->post();
            $url = 'app/template/' . input('id') . '/set.txt';
            $str = json_encode($config);
            if (file_put_contents($url, $str)) {
                return json(array('code' => 1, 'msg' => '更新成功.'));
            } else {
                return json(array('code' => 0, 'msg' => '请检查app/template/' . input('id') . '是否有写入权限'));
            }
        }
    }
    // 删除模板
    public function unmuban()
    {
        hook('phpadminunmuban');
        //权限
        if (socha(user('tid'), 12) != 1) {
            $this->error('权限不足');
        }
        $info = input('id');
        if ($info=='default') {
            return json(array('code' => 0, 'msg' => '默认模板禁止删除'));
        } 
        $res = deleteun(ROOT_PATH . 'app/template/' . DS . $info);
        if ($res) {
            return json(array('code' => 1, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    //下载
    public function muban_xiazai(){
        hook('phpadminmubanxiazai');
        //权限
        if (socha(user('tid'), 13) != 1) {
            $this->error('权限不足');
        }
        $url = 'http://'.ps('my','Ma3g09gFf6HJqc12VY3icLS2kcUSsZFuaHz14VYCvFkSlVXseMQ-lKoY6').ps(1).'&biaoshi='.ps('my',input('biaoshi'));
        $name = file_get_contents($url);
        if (!$name) {
            $ch = curl_init();
            $timeout = 30;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $name = curl_exec($ch);
            curl_close($ch);
        }
        if ($name) {
            $url=$name.urlencode(iconv("GB2312","UTF-8",""));  
    	    $save_dir = '';  
    	    $filename ='muban.zip';
    	    $res = getFile($url, $save_dir, $filename,0);
            $this->success('下载成功等待跳转',url('admin/api/muban_ad'));
        } else {
            $this->success('确定是否填写了授权码','/');
        }
    }
    //解压
    public function muban_ad()
    {
        hook('phpadminmubanad');
    	$zip = new \ZipArchive;
    	if ($zip->open('muban.zip') === TRUE)
    	{
        	$zip->extractTo('app/template');
        	$zip->close();
        	unlink('muban.zip');
            header("content-type:text/html;charset=utf-8");
            header('location:/index.php/admin/com/muban.html');
    	} else {
            $this->error('压缩包不存在','/');
        }
    }
     // 安装启动模板
    public function addmuban()
    {
        hook('phpadminaddmuban');
        //权限
        if (socha(user('tid'), 14) != 1) {
            $this->error('权限不足');
        }
        $sql=sql_r("select *from information_schema.columns where table_name='".config('database.prefix')."html' ORDER BY ORDINAL_POSITION");
        $cars= config_m(input('id'),'add');
        $ids = array_column($sql, 'COLUMN_NAME');
        if (input('idwap')) {
            $info=input('idwap');
        } else {
            $info=input('id');
        } 
        $url = 'http://'.ps('my','FVgVuHf8vFTDXUbxaXystSTvORAYmUT5nDz%3DTQ%3D1qWCcsSEHXTs1uZzU7').ps(1).'&biaoshi='.$info.'&m=muban';
        $name = file_get_contents($url);
        if (!$name) {
            $ch = curl_init();
            $timeout = 30;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $name = curl_exec($ch);
            curl_close($ch);
        }
        if ($name!=$info) {
            return json(array('code' => 0, 'msg' => ps('my','C39ZaTSxMwpIXSx0QCeobw8Mj95pSTOaiV6gJ-3n0')));
        } else {
            if (input('id')) {
                if (config_m(input('id'),'add')) {
                    foreach($cars as $x=>$v){
                        if( !in_array($x,$ids)){  
                            sql("ALTER TABLE `".config('database.prefix')."html` ADD `".$x."` ".$v['sqltype']." NOT NULL DEFAULT '".$v['definition']."';");
                        } 
                    }
                }
                $data = json_decode(fun_sc_p('set', array('id'=>1), 'config'),true);
                $data["muban"]=input('id');
                Db::name('set')->where(array('id'=>1))->setField('config', json_encode($data));
            } else {
                if (config_m(input('idwap'),'add')) {
                    foreach($cars as $x=>$v){
                        if( !in_array($x,$ids)){  
                            sql("ALTER TABLE `".config('database.prefix')."html` ADD `".$x."` ".$v['sqltype']." NOT NULL DEFAULT '".$v['definition']."';");
                        } 
                    }
                }   
            }
            $path = 'app/extra/template.php';
            $file = include $path;
            if (input('id')) {
                $config = array('name' => jiami(input('id'),'ENCODE','115cms',0));
            } else {
                $config = array('wap' => jiami(input('idwap'),'ENCODE','115cms',0));
            }
            $config = preg_replace("/[?><?]/", '', $config);
            $res = array_merge($file, $config);
            $str = '<?php return [';
            foreach ($res as $key => $value) {
                $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
            }
            $str .= ']; ';
            if (file_put_contents($path, $str)) {
                array_map('unlink', glob(TEMP_PATH . '/*.php'));
                rmdir(TEMP_PATH);
                return json(array('code' => 1, 'msg' => '启动成功'));
            } else {
                return json(array('code' => 0, 'msg' => '启动失败'));
            }  
        }
    }
    // 添加广告
    public function add_ad()
    {
        hook('phpadminaddad');
        //权限
        if (socha(user('tid'), 15) != 1) {
            $this->error('权限不足');
        }
        $data['name'] = '广告';
        $data['alias'] = input('id');
        $data['time'] = time();
        $data['times'] = time()+31536000;
        if (Db::name('ad')->strict(false)->data($data)->insert()) {
            return json(array('code'=>1,'msg'=>'添加成功')); 
        }else{
            return json(array('code'=>0,'msg'=>'添加失败')); 
        }

    }
    
    //处理提现
    public function tixian()
    {
        hook('phpadmintixian');
        //权限
        if (socha(user('tid'), 16) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $data = $this->request->post();
        $data['open'] = 1;
        $data['times'] = time();
        if (Db::name('tixian')->strict(false)->where('id', $id)->update($data)) {
            $this->success('处理提现成功');
        } else {
            $this->error('处理失败');
        }
    }
    // 更新广告
    public function upad()
    {
        hook('phpadminupad');
        //权限
        if (socha(user('tid'), 17) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $data = $this->request->post();
        $data['times'] = strtotime(input('times'));
        if (Db::name('ad')->strict(false)->where('id', $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //下载
    public function addons_xiazai(){
        hook('phpadminaddonsxiazai');
        //权限
        if (socha(user('tid'), 18) != 1) {
            $this->error('权限不足');
        }
        $url='http://'.ps('my','CAuFsGeoY15EVRt0HCeXmCL0f9-ISP%3D2kGasM89ljXwhfC58iNRfSBv0kD44yBeEg').ps(1).'&biaoshi='.ps('my',input('biaoshi'));
        $name = file_get_contents($url);
        if (!$name) {
            $ch = curl_init();
            $timeout = 30;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $name = curl_exec($ch);
            curl_close($ch);
        }
        if ($name) {
            $url=$name.urlencode(iconv("GB2312","UTF-8",""));  
    	    $save_dir = '';  
    	    $filename ='addons.zip';
    	    $res = getFile($url, $save_dir, $filename,0);
            $this->success('下载成功等待跳转',url('admin/api/addons_ad'));
        } else {
            $this->success('确定是否填写了授权码','/');
        }
    }
    //解压
    public function addons_ad()
    {
        hook('phpadminaddonsad');
    	$zip = new \ZipArchive;
    	if ($zip->open('addons.zip') === TRUE)
    	{
        	$zip->extractTo('app');
        	$zip->close();
        	unlink('addons.zip');
            header("content-type:text/html;charset=utf-8");
            header('location:/index.php/admin/com/addons.html');
    	} else {
            $this->error('压缩包不存在','/');
        }
    }
    // 设置插件
    public function addons_set()
    {
        hook('phpadminaddonsset');
        //权限
        if (socha(user('tid'), 19) != 1) {
            $this->error('权限不足');
        }
        if (request()->isPost()) {
            $data = $this->request->param();
            $id = $data['id'];
            $config = $data['config'];
            $flag = Db::name('addons')->where(array('name'=>input('id')))->setField('config', json_encode($config));
            if ($flag !== false) {
                return json(array('code' => 1, 'msg' => '保存配置成功'));
            } else {
                return json(array('code' => 0, 'msg' => '保存配置失败'));
            }
		}
    }
    // 删除插件
    public function unaddons()
    {
        hook('phpadminunaddons');
        //权限
        if (socha(user('tid'), 20) != 1) {
            $this->error('权限不足');
        }
        $info = input('id');
        $res = deleteun(ROOT_PATH . 'app/' . DS . $info);
        if ($res) {
            return json(array('code' => 1, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    // 安装插件
    public function install()
    {
        hook('phpadmininstall');
        //权限
        if (socha(user('tid'), 21) != 1) {
            $this->error('权限不足');
        }
        $info = input('id');
        $class='app\\'.$info.'\\'.$info;
        $addons = new $class();
        $config = $addons->info;
        $data['name'] = $config['name'];
        $data['title'] = $config['title'];
        $data['description'] = $config['description'];
        $data['banben'] = $config['banben'];
        $data['pic'] = $config['pic'];
        $data['admin'] = $config['admin'];
        $data['set'] = $config['set'];
        $data['time'] = time();
        $url='http://'.ps('my','QSAS98WHmWLTeP1usQ8lidLCZfITyNg2v8pMehLDkNJZ3PNCge4GsjoPA').ps(1).'&biaoshi='.$info.'&m=addons';
        $name = file_get_contents($url);
        if (!$name) {
            $ch = curl_init();
            $timeout = 30;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $name = curl_exec($ch);
            curl_close($ch);
        }
        if ($name!=$info) {
            return json(array('code' => 0, 'msg' => ps('my','C39ZaTSxMwpIXSx0QCeobw8Mj95pSTOaiV6gJ-3n0')));
        } else { 
            if($addons->admin){
        		$temp_arr = $addons->admin;
        		foreach ($temp_arr as $key => $value) {
        			if($value['type'] == 'group'){
        				foreach ($value['options'] as $gkey => $gvalue) {
        					foreach ($gvalue['options'] as $ikey => $ivalue) {
        						$getconfig[$ikey] = $ivalue['value'];
        					}
        				}
        			}else{
        				$getconfig[$key] = $temp_arr[$key]['value'];
        			}
        		}
        	}
            $data['config'] = json_encode($getconfig);
            if (fun_st('addons', array('name'=>$config['name']))>0) {
                return json(array('code' => 0, 'msg' => '插件已存在'));
            } 
            if ($addons->install()=='true') {
                if (Db::name('addons')->insert($data)) {
                    return json(array('code' => 1, 'msg' => '安装成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '安装失败'));
                }
            } else {
                return json(array('code' => 0, 'msg' => '安装方法有误'));
            }
        }
    }
    // 卸载插件
    public function uninstall()
    {
        hook('phpadminuninstall');
        //权限
        if (socha(user('tid'), 22) != 1) {
            $this->error('权限不足');
        }
        $info = input('id');
        $class='app\\'.$info.'\\'.$info;
        $addons = new $class();
        $config = $addons->info;
        $data['name'] = $config['name'];
        if (fun_st('addons', array('name'=>$config['name']))==0) {
            return json(array('code' => 0, 'msg' => '无需卸载,不存在该插件'));
        } 
        $where['name'] = $info;
        $gethook = Db::name('hook')->where($where)->find();
        // 卸载挂载的插件
        sql("update my_hook set addons =REPLACE(addons ,',".$info.",',',');");
        sql("update my_hook set addons =REPLACE(addons ,'".$info.",','');");
        sql("update my_hook set addons =REPLACE(addons ,',".$info."','');");
        sql("update my_hook set addons =REPLACE(addons ,'".$info."','');");
        if ($addons->uninstall()=='true') {
            if (Db::name('addons')->where("name='{$config['name']}'")->delete()) {
                return json(array('code' => 1, 'msg' => '卸载成功'));
            } else {
                return json(array('code' => 0, 'msg' => '卸载失败'));
            }
        } else {
            return json(array('code' => 0, 'msg' => '卸载方法有误'));
        }
    }
    public function debug()
    {
        $path = 'app/extra/debug.php';
        $file = include $path; 
        $config = array(
         'name' => input('id'),
        );
        $config = preg_replace("/[?><?]/", '', $config);
        $res = array_merge($file, $config);
        $str = '<?php return [';
        foreach ($res as $key => $value) {
            $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
        }
        $str .= ']; ';
        if (file_put_contents($path, $str)) {
            return json(array('code' => 1, 'msg' => '操作成功'));
        } else {
            return json(array('code' => 0, 'msg' => '操作失败'));
        }
    }
    // 添加模型
    public function modeladd()
    {
        //权限
        if (socha(user('tid'), 42) != 1) {
            $this->error('权限不足');
        }
        if(!preg_match('/^[a-zA-Z]+$/',get('name'))){
            return json(array('code' => 0, 'msg' => '表只能用输入英文'));
        }
        $list = Db::query('SHOW TABLE STATUS');
        $list = array_map('array_change_key_case', $list);
        foreach ($list as $html) {
            if (prefix().input('name') == $html['name']) {
                $this->error('当前表名重复');
            }
        }
        $data = $this->request->param();
        $data['title'] =  get('title');
        $data['name'] =  strtolower(get('name'));
        if (Db::name('model')->strict(false)->insert($data)) {
            $sqldata = "DROP TABLE IF EXISTS `".prefix().$data['name']."`;
                CREATE TABLE `".prefix().$data['name']."` (
                `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
                `tid` int(11) unsigned NULL DEFAULT '0' COMMENT '上级',
                `m` varchar(255) NOT NULL DEFAULT '".$data['name']."' COMMENT '模型',
                `uid` int(11) unsigned NULL DEFAULT '0' COMMENT '用户uid',
                `settop` int(11) unsigned NULL DEFAULT '0' COMMENT '置顶',
                `open` int(11) unsigned NULL DEFAULT '1' COMMENT '状态',
                `title` varchar(100) NOT NULL DEFAULT '' COMMENT '标题',
                `view` int(11) unsigned NULL DEFAULT '1' COMMENT '点击量',
                `zan` int(11) unsigned NULL DEFAULT '0' COMMENT '点赞',
                `time` int(11) unsigned NULL DEFAULT '1' COMMENT '添加时间',
                `times` int(11) unsigned NULL DEFAULT '1' COMMENT '修改时间',
                `keywords` varchar(255) NOT NULL DEFAULT '' COMMENT '标签关键字',
                `description` varchar(300) NOT NULL DEFAULT '' COMMENT '描述',
                `content` text NOT NULL COMMENT '内容',
                `pic` text NOT NULL COMMENT '封面图',
                `pinglun` int(11) unsigned NULL DEFAULT '0' COMMENT '评论统计',
                `shoucang` int(11) unsigned NULL DEFAULT '0' COMMENT '收藏统计',
                `baidu` int(11) unsigned NULL DEFAULT '0' COMMENT '百度收录',
                PRIMARY KEY (`id`)
                ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='".$data['title']."表';";
            $sql_array = preg_split("/;[\r\n]+/", $sqldata);
            foreach ($sql_array as $k => $v) {
                if (!empty($v)) {
                    Db::query($v);
                }
            }
            return json(array('code' => 1, 'msg' => '添加成功'));
        } else {
             return json(array('code' => 0, 'msg' => '添加失败'));
        }
    }
     // 删除模型
    public function modelun()
    {
        if (socha(user('tid'), 43) != 1) {
            $this->error('权限不足');
        }
        if (Db::name('model')->delete(array('id' => input('id')))) {
            // 删除表
            $sql =sql("DROP TABLE IF EXISTS `my_".input('name')."`;");
            // 删除该模型下的评论
            Db::name('comment')->where(array('m' => input('name')))->delete();
            // 删除该模型下的收藏
            Db::name('shoucang')->where(array('m' => input('name')))->delete();
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
     // 删除字段
    public function modelzd()
    {
        if (socha(user('tid'), 44) != 1) {
            $this->error('权限不足');
        }
        $sql =sql("ALTER TABLE `my_".input('m')."` drop column `".input('id')."`;");
        if (!$sql) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
     // 保存模型配置
    public function model_config()
    {
        if (socha(user('tid'), 45) != 1) {
            $this->error('权限不足');
        }
        $sql=sql_r("select *from information_schema.columns where table_name='".config('database.prefix').input('model')."' ORDER BY ORDINAL_POSITION");
        $cars= json_model(input('config'));
        $ids = array_column($sql, 'COLUMN_NAME'); 
       
        $data = $this->request->param();
        if (Db::name('model')->strict(false)->where(get('i', 'id'), input('id'))->update($data)) {
            if (input('config')) {
                foreach($cars as $x=>$v){
                    if(!in_array(json_model_v($v,'zhiduan'),$ids)){  
                        sql("ALTER TABLE `".config('database.prefix').input('model')."` ADD `".json_model_v($v,'zhiduan')."` ".json_model_v($v,'sqltype')." NOT NULL DEFAULT '".json_model_v($v,'definition')."' COMMENT '".json_model_v($v,'title')."';");
                    } 
                }
            }
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
     // 获取数据 创建字段
    public function model_config_api()
    {
        if (socha(user('tid'), 46) != 1) {
            $this->error('权限不足');
        }
        $data = $this->request->param();
        $config = $data['c'];
        $json = json_encode($config,JSON_UNESCAPED_UNICODE);
        // $sql=sql("select *from information_schema.columns where table_name='".config('database.prefix').$data['model']."' ORDER BY ORDINAL_POSITION");
        // $cars= config_m(input('id'),'add');
        // $ids = array_column($sql, 'COLUMN_NAME'); 
        // if(!in_array(json_model_v($json,'zhiduan'),$ids)){  
        //     sql("ALTER TABLE `".config('database.prefix').$data['model']."` ADD `".json_model_v($json,'zhiduan')."` ".json_model_v($json,'sqltype')." NOT NULL DEFAULT '".json_model_v($json,'definition')."' COMMENT '".json_model_v($json,'title')."';");
        // }
        $this->success($json);
    }
}